package com.example.demo.controller;

/**
 * @ClassName: IndexController
 * @Description:
 * @Author: 12071
 * @Date: 2021/4/11 10:23
 */

import com.example.demo.bean.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    JdbcTemplate jdbcTemplate;

    /**
     * 去登录页
     * @return
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){
        if(!StringUtils.isEmpty(user.getUserName())  && "123456".equals(user.getPassword())){
            //把登陆成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登陆成功重定向到/main.html;
            // 防止页面重新提交：重定向
            return "redirect:/main.html";
        }else{
            //登陆失败
            model.addAttribute("msg","账号密码错误");
            return "login";
        }
    }

    /**
     * 去main页面
     * 路径的.html 表示直接跳转到页面
     * @return
     */
    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model){
        /*//是否登录：最好采用拦截器、过滤器
        //有拦截器后不再需要
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser != null){
            return "main";
        }else{
            model.addAttribute("msg","请先登录");
            return "login";
        }*/
        return "main";
    }

    @GetMapping(value = {"img"})
    public String loadImg(Model model,
                          @RequestParam(value = "mapId") Integer mapId){
        model.addAttribute("imagePath","/images/zjut.png");
        return "imageTest";
    }

    @GetMapping("/socketTest")
    public String socketPage(){
        return "socket";
    }

    @GetMapping("/basic")
    public String basicTablePage(){
        return "table/basic_table";
    }
}
